package org.example.java8.optional;

import java.util.Optional;

/**
 * @author 夏焰波
 * @date 2020-07-13 23:33
 */
//you just tell "how" not "do what".
public class OptionalInAction {
    public static void main(String[] args) {

        Optional.ofNullable(getInsuranceNameByOptional(null)).ifPresent(System.out::println);
    }

    private static String getInsuranceNameByOptional(Person person) {
        //Optional<Optional<Car>> car = Optional.ofNullable(person).map(Person::getCar);
        //.map(Car::getInsurance)
        //.map(Insurance::getName);

        Optional<Car> car = Optional.ofNullable(person).flatMap(Person::getCar);
        String s = car.flatMap(Car::getInsurance).map(Insurance::getName).orElse("unknown");

        return s;
    }
}
